local joy__anshi = fk.CreateSkill {

  name = "joy__anshi",

  tags = { Skill.Compulsory, },

}



joy__anshi:addEffect(fk.RoundStart, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})
joy__anshi:addEffect(fk.CardUsing, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})
joy__anshi:addEffect(fk.CardResponding, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})
joy__anshi:addEffect(fk.AfterCardsMove, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})
joy__anshi:addEffect(fk.TargetSpecifying, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})
joy__anshi:addEffect(fk.RoundEnd, {
  name = "joy__anshi",
  anim_type = "control",
  events = {fk.RoundStart, fk.CardUsing, fk.CardResponding, fk.AfterCardsMove, fk.TargetSpecifying, fk.RoundEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__anshi.name) then return end
    if event == fk.RoundStart then return true end
    local mark = player:getMark("@[joy__anshi]joy__anshi-round")
    if not mark then return end
    if event == fk.AfterCardsMove then
      if mark ~= 3 then return end
      local list = {}
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerEquip then
          table.insertIfNeed(list, move.to)
        end
        if move.from and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerEquip end) then
          table.insertIfNeed(list, move.from)
        end
      end
      list = table.filter(list, function(pid) return #player.room:getPlayerById(pid).player_cards[Player.Equip] > 0 end)
      if #list > 0 then
        player.room:sortPlayersByAction(list)
        event:setCostData(self, list)
        return true
      end
    elseif event == fk.TargetSpecifying then
      return mark == 5 and data.card.type == Card.TypeTrick and data.firstTarget and data.card:isCommonTrick() and not data.card.multiple_targets
    elseif event == fk.RoundEnd then
      return mark == 2
    else
      if mark == 1 then
        return not target.dead and data.card.trueName == "slash"
      elseif mark == 4 then
        return not target.dead and (data.card.name == "peach" or data.card.name == "analeptic")
        and event == fk.CardUsing
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local n = math.random(5)
      room:setPlayerMark(player, "@[joy__anshi]joy__anshi-round", n)
    else
      local mark = player:getMark("@[joy__anshi]joy__anshi-round")
      if mark == 1 then
        target:throwAllCards("h")
      elseif mark == 2 then
        local players = table.filter(room:getAlivePlayers(), function (p)
          return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
            return e.data.from == p and e.data.card.name == "jink"
          end, Player.HistoryRound) == 0
          and
          #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
              if move.from==p and move.moveReason == fk.ReasonDiscard then
                for _, info in ipairs(move.moveInfo) do
                  if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                  and Fk:getCardById(info.cardId).name == "jink" then
                    return true
                  end
                end
              end
            end
          end, Player.HistoryRound) == 0
        end)
        for _, p in ipairs(players) do
          if not p.dead then
            room:damage { from = nil, to = p, damage = 1, skillName = joy__anshi.name, damageType = fk.ThunderDamage }
          end
        end
      elseif mark == 3 then
        for _, pid in ipairs(event:getCostData(self)) do
          room:getPlayerById(pid):throwAllCards("e")
        end
      elseif mark == 4 then
        room:setPlayerMark(target, "@@joy__anshi_prohibit-turn", 1)
      elseif mark == 5 then
        player:drawCards(1, joy__anshi.name)
      end
    end
  end,
})

joy__anshi:addEffect("prohibit", {
  name = "#joy__anshi_prohibit",
  prohibit_response = function(self, player, card)
    if player:getMark("@@joy__anshi_prohibit-turn") > 0 then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player.player_cards[Player.Hand], id)
      end)
    end
  end,
  prohibit_use = function(self, player, card)
    if player:getMark("@@joy__anshi_prohibit-turn") > 0 then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player.player_cards[Player.Hand], id)
      end)
    end
  end,
})

return joy__anshi